R Beginners Course

Introduction to R - Part 1

Objective and contents

Objective and contents

Objective: to generate the basic knowledge for data analysis and interpretation using the R Project for Statistical Computing

Contents:

  • R and RStudio
  • Introduction to commands and assignment of variables
  • Vectors and sequences
  • Objects and their modes and attributes
  • Missing values
  • Some functions
  • Operators in R

Objective and contents

Contents:

  • Index vectors
  • Matrices, lists, and data frames
  • Scripts and packages
  • Basic functions
  • Arithmetic operators

What is R?

Short introduction

The use of a programming language is useful when:

  • Dealing with large datasets
  • Dealing with data with high spatial resolution
  • The method(s) must be applied over several regions
    • Or applied over multiple time steps
  • We want to achieve efficiency and automation
  • … and also for your employability after graduation!

R and RStudio

R is a language and environment for statistical computing and graphics.

  • R provides statistical and graphical techniques
  • R incorporates statistical tests, models, and analyses
  • R was developed by statisticians and researchers
  • R is free and open source
  • R can be run in parallel (multi-core machines or network clusters)

Installation

R is an open source software and can be downloaded for Linux, OS X (Mac) or Windows

Graphical User Interface

A Graphical User Interface allows the users to interact with electronic devices through graphical icons instead of text-based user interfaces.

Image

RStudio

RStudio is an integrated development environment for R, which is available in open source and commercial editions and runs on Windows, Mac, and Linux.

Image

Scripts

An R script is simply a text file containing (almost) the same commands that would be entered on the command line of R. It allows the user to compute the same set of actions continuously.

Image

R Packages

Packages are collections of R functions, data, and complex code in a well-defined format.

  • R has several packages already loaded
  • If a new package is needed, it can be downloaded from CRAN, a repository where R packages are available

As an example, we can install the terra package:

install.packages("terra")

R Packages

The installed packages must be loaded in order to use their functions. For this purpose, the function library is used

library("terra")

To remove an existing package:

remove.packages(terra)

To visualise the installed packages and the packages in use:

library()
search()

Data in R

R is an interpreted language

  • This means that each written command is directly executed while all the data (variables and results) are stored as objects (everything in R is an object)
  • The functions used in R are executed writing commands

The result of each command can be:

  1. visualised directly on the screen
  2. stored in an object
  3. written on the hard disk

R commands

R commands

R is an expression language and is case sensitive; therefore x and X are different objects that refer to different variables

x <- 10
X <- "This is a string"
print(x)
## [1] 10
print(X)
## [1] "This is a string"

The use of comments is very useful to know precisely what is happening in the script. The comments should always begin with the symbol #

# This is a very useful comment
x <- 5 * 45

If a command is not complete, R will display the + symbol. It indicates that something is missing. In this case you can complete the command or press Esc to To cancel its execution.

Recall of commands and data in R

To recall and execute previous commands, you can use the vertical arrow keys on the keyboard. This helps to apply or correct previous executed functions.

The data can be either visualised on the screen:

5*5
## [1] 25

Stored in an object:

x <- 5*5
x <- "Hello world"

Recall of commands and data in R

Or visualised on the screen by a plot:

x <- 1:10; y <- 1:10
plot(x, y)

Assignment of variables

There are many ways of assigning a value to a variable. The most common is the <- operator, which is composed by the less than character (<) and the minus character (-).

n <- 5
print(n)
## [1] 5

The = operator can be used as well.

n <- "Mexico"
print(n)
## [1] "Mexico"

Assignment of variables

Additionally, the assign function can be also used.

assign("n", 100)
print(n)
## [1] 100

assign("a", "Hello guys!")
print(a)
## [1] "Hello guys!"

Vectors and sequences

Vectors and sequences

A numeric vector is a ordered collection of numbers. To set up a vector is necessary to use the c( ) function.

x <- c(1, 2, 3, 4, 8, 5, 7)
print(x)
## [1] 1 2 3 4 8 5 7

Similarly, we can create vectors that contain characters:

x <- c("Oscar", "Manuel", "Baez", "Villanueva")
print(x)
## [1] "Oscar"      "Manuel"     "Baez"       "Villanueva"

Vectors and sequences

Sometimes, we need to create long vectors that follow a know sequence. For example:

x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
print(x)
##  [1]  1  2  3  4  5  6  7  8  9 10

For this purpose we can use the : symbol, which will create a sequence of consecutive integers.

x <- 1:10
print(x)
##  [1]  1  2  3  4  5  6  7  8  9 10

Vectors and sequences

Other option is to use the seq function, which will create a sequence according to the desired step.

seq(from = 1, to = 1, by = ((to - from)/(length.out - 1)),
    length.out = NULL, along.with = NULL, ...)

For example:

x <- seq(from = 1, to = 10, by = 1)
print(x)
##  [1]  1  2  3  4  5  6  7  8  9 10
x <- seq(1, 10, 0.5)
print(x)
##  [1]  1.0  1.5  2.0  2.5  3.0  3.5  4.0  4.5  5.0  5.5  6.0  6.5  7.0  7.5  8.0
## [16]  8.5  9.0  9.5 10.0

Mode and attributes

Mode

Objects are characterised by their name, content, and attribute. They represent the data stored in them. The mode function identifies the kind of elements in the object. Each object has only one mode. There are 4 principal modes (atomic modes):

  1. Numeric (e.g., 1, -5, 435, 28)
  2. Character (e.g., “Hello”, “B”, “Oscar”)
  3. Logical (i.e., TRUE and FALSE)
  4. Complex (e.g., 1i, 10i, 1\(\sqrt{-1}\))

Length

The command length indicates the amount of elements in an object.

x <- "Evaporation"
length(x)
## [1] 1
mode(x)
## [1] "character"

Which mode and length correspond to these elements?

Object Mode Length
58i
Cadmium
1:10
TRUE

Length

The command length indicates the amount of elements in an object.

x <- "Evaporation"
length(x)
## [1] 1
mode(x)
## [1] "character"

Which mode and length correspond to these elements?

Object Mode Length
58i complex 1
Cadmium character 1
1:10 numeric 10
TRUE logical 1

Class

All objects in R have a class, which can be retrieved with the class function. In the case of simple objects, the class is the same as the mode (e.g., “numeric”, “character”, and “logical”). For more complex object, the class will differ (e.g., “list”, “array”, “matrix”, and “data.frame”).

a <- 1:100
class(a)
## [1] "integer"
b <- c(TRUE, FALSE, TRUE)
class(b)
## [1] "logical"
# Class from a predefined object
class(mtcars)
## [1] "data.frame"

Missing data

In some cases, there will be missing data in a set of values. These missing values are represented by the NA string. Please note that this is not a character.

x <- c(1, 5, 7, NA, 8, 9)
mode(x)
## [1] "numeric"
x <- c(1, 5, 7, "NA", 8, 9)
mode(x)
## [1] "character"

Infinite and Not a Number

Inf represents a non-finite numeric value.

1/0
## [1] Inf
log(0)
## [1] -Inf

Not a Number (NaN) represents values that should be numeric but they are not.

sqrt(-1)
## Warning in sqrt(-1): NaNs produced
## [1] NaN
log(-5)
## Warning in log(-5): NaNs produced
## [1] NaN

Some functions

Some functions

abs: absolute value of the object.

abs(-1678)
## [1] 1678

^n: raise to the power of n.

2^3
## [1] 8

sqrt: square root of the object.

sqrt(25)
## [1] 5

Some functions

log: natural logarithm.

log(100)
## [1] 4.60517

exp: antilogarithm.

exp(4.6051706)
## [1] 100

rep: creates a vector of n times the object.

rep("Hi", times = 6)
## [1] "Hi" "Hi" "Hi" "Hi" "Hi" "Hi"

Some functions

basename: returns the part of the file path after the last separator (/).

data.path <- "C:/Users/Documents/WS_Sam_Neua/WS1_proj.tif"
basename(data.path)
## [1] "WS1_proj.tif"

dirname: returns the part of the file path before the last separator (/).

dirname(data.path)
## [1] "C:/Users/Documents/WS_Sam_Neua"

Some functions

paste: used to paste multiple text strings together as one.

paste("How", "are you?")
## [1] "How are you?"
num.days <- 31
paste("January has", num.days, "days.")
## [1] "January has 31 days."

Some functions

paste0: used to paste multiple text strings together as one without spaces.

year <- 2010
month <- 12
file.name <- paste0("C:/Users/Documents/Result_", year, "-", month)
print(file.name)
## [1] "C:/Users/Documents/Result_2010-12"

Some functions

file.path: Similar to paste and paste0, but will add forward slashes between each input.

file.path("C:/Users", "Folder")
## [1] "C:/Users/Folder"

substr: used to extract (or replace) substrings in a character vector.

substr("Pink Floyd", 2, 6)
## [1] "ink F"

file.name <- "C:/Users/Documents/Result_2010-12.tif"
year <- substr(file.name, 27, 30)
print(year)
## [1] "2010"

Some functions

For this second example, we could combine the basename function as follows:

yr <- substr(basename(file.name), 8, 11)
print(yr)
## [1] "2010"
Why could this be beneficial?

Some functions

To list files inside a folder, the function list.files can be used. Please copy the path from a folder on your computer.

list.files(path = ".", pattern = NULL, all.files = FALSE,
           full.names = FALSE, recursive = FALSE,
           ignore.case = FALSE, include.dirs = FALSE, no.. = FALSE)

For Example:

list.files("C:/User/Data/L2_Introduction_II_Data/Cities")
## [1] "Italy.csv" "Spain.csv"

Some functions

list.files("C:/User/Data/L2_Introduction_II_Data/Cities", full.names = TRUE)
## [1] "../Data/L2_Introduction_II_Data/Cities/Italy.csv"
## [2] "../Data/L2_Introduction_II_Data/Cities/Spain.csv"

Sys.time: Prints the time and date. This can be useful when running long scripts, so that you can work out how long a step or an entire script has taken to execute.

Sys.time()
## [1] "2022-04-28 16:36:53 CEST"

Some functions

t: transposes a matrix.

example <- matrix(c(1:20), nrow = 5)
print(example)
##      [,1] [,2] [,3] [,4]
## [1,]    1    6   11   16
## [2,]    2    7   12   17
## [3,]    3    8   13   18
## [4,]    4    9   14   19
## [5,]    5   10   15   20
example.trans <- t(example)
print(example.trans)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    2    3    4    5
## [2,]    6    7    8    9   10
## [3,]   11   12   13   14   15
## [4,]   16   17   18   19   20

Some functions

cumsum: gives the cumulative sum from a vector.

cumsum(c(1, 2, 3, 4))
## [1]  1  3  6 10

log10: logarithm base 10.

log10(100)
## [1] 2

Getting help

The function help.start will launch a Web browser that allows the help pages to be browsed with hyperlinks.

help.start()

You don’t need to memorise all information to include for each function. You can search the R documentation for a specific term with the question mark (?) before the function name.

?sum

Getting help

Additionally, the double question marks (??) search the R help files for a word or phrase.

??sum

Operators in R

Operators in R

Two vectors can be added, subtracted, multiplied and divided:

x <- 1:10
y <- 11:20
x + y
##  [1] 12 14 16 18 20 22 24 26 28 30
x - y
##  [1] -10 -10 -10 -10 -10 -10 -10 -10 -10 -10
x * y
##  [1]  11  24  39  56  75  96 119 144 171 200
x / y
##  [1] 0.09090909 0.16666667 0.23076923 0.28571429 0.33333333 0.37500000
##  [7] 0.41176471 0.44444444 0.47368421 0.50000000

Operators in R

range: gives the minimum and maximum values from a vector.

range(x)
## [1]  1 10

The functions max, min, mean, and var give the maximum, minimum, mean and variance values from a vector, respectively.

x <- -10:10
max(x)
## [1] 10
min(x)
## [1] -10
mean(x)
## [1] 0
var(x)
## [1] 38.5

Operators in R

sum: gives the sum of any vector.

sum(c(1, 2, 3, 4))
## [1] 10

prod: gives the product of any vector.

prod(c(1, 2, 3, 4))
## [1] 24

Operators in R

sort: sorts the values increasing (by default) or decreasing (decreasing = TRUE).

y <- c(5, 3, 6, 8, 3, -1)
sort(y)
## [1] -1  3  3  5  6  8
sort(y, decreasing = TRUE)
## [1]  8  6  5  3  3 -1

Please sort the following vector in an increasing and decreasing manner:

5, 9, 12, -10, -5, 3, 900

Operators in R

order: this works similar to the sort function, but rather that returning the reordered values themselves, it returns the positions of these values.

y <- c(5, 3, 6, 8, 3, -1)
order(y)
## [1] 6 2 5 1 3 4

Operators in R

When can we use order instead of sort?

Index vectors

The squared brackets ([]) can be used to subset elements from a vector.

x <- c(10, 12, 13, 13, 17, 18, 18, 18, 21, 22)
x[1]
## [1] 10
x[c(1, 2, 3)]
## [1] 10 12 13

To extract everything but one or more selected element or elements, we can use the minus symbol inside the square brackets:

x[-1]
## [1] 12 13 13 17 18 18 18 21 22
x[-c(1, 4, 7)]
## [1] 12 13 17 18 18 21 22

Index vectors

From the same vector please extract:

  • The fifth element
  • The third, sixth, and eighth
  • Everything except the tenth

Unique Values in a Vector

To show all the unique values in a vector, we can use the unique function.

x <- c(10, 12, 13, 13, 17, 18, 18, 18, 21, 22)
unique(x)
## [1] 10 12 13 17 18 21 22

The which function

In order to find the position of a specific object (e.g., numerical values, character values) in a vector, the which function is used. - If the requested object does not exist in the vector, the output vector has no values inside it

x <- c(10, 12, 13, 13, 17, 18, 18, 18, 21, 22)
which(x == 18)
## [1] 6 7 8
which(x > 17)
## [1]  6  7  8  9 10
which(x == 9)
## integer(0)

The which function

To know the position of values that are not equal to a specific object, we can use the which function together with the logical operator not equal to “!=”.

x <- c(10, 12, 13, 13, 17, 18, 18, 18, 21, 22)
which(x != 18)
## [1]  1  2  3  4  5  9 10

Note: when you want to search for multiple values within an object, it is better to use the %in% function (see Lecture 4).

Matrices, lists, and data frames

Matrices

A matrix is a bidimensional array. It can be created with the matrix function.

# Matrix with 5 rows and 2 columns and number 10 as a value
x <- matrix(10, nrow = 5, ncol = 2)
print(x)
##      [,1] [,2]
## [1,]   10   10
## [2,]   10   10
## [3,]   10   10
## [4,]   10   10
## [5,]   10   10

Matrices

With the rnorm function, we can create random values with a normal distribution. We can use this function to create a matrix of 60 random values:

# Matrix with 12 rows and 5 columns and random values
x <- matrix(rnorm(60), nrow = 12)
print(x)
##              [,1]        [,2]         [,3]        [,4]       [,5]
##  [1,]  1.34185154  0.20359985 -0.005034602 -0.76186135 -1.0489687
##  [2,]  0.27001872  1.64266804  1.361292452  0.10048312 -0.8730561
##  [3,]  0.35729918 -1.02312092  1.514999869  0.42742738 -0.4395550
##  [4,]  2.02984054  0.40941469  1.078059341  1.12477940  0.3196170
##  [5,] -0.29178567 -0.31932897 -1.233155710 -0.48092053  0.3287599
##  [6,] -1.33367146 -0.03555508  0.008222698 -0.33403233 -0.1328199
##  [7,] -1.07762081 -1.01259814  0.804670049  0.53818671 -1.8376158
##  [8,] -0.68245981 -0.92572637  0.487013911 -0.13250592  0.1971838
##  [9,] -0.44350603  0.82192561  0.792913088  0.65469014 -0.9422564
## [10,]  0.48617600 -0.05174419 -0.700349933 -0.05280361  0.4678160
## [11,]  2.10351000  0.42367104  0.742110651  0.48480483  0.2923420
## [12,] -0.01478887  0.47969944 -0.045532817 -1.63538774  1.8060488

Matrices

Extracting values from a matrix is similar to extracting values from a vector; however, a matrix has two dimensions [row number, column number].

print(x)
##              [,1]        [,2]         [,3]        [,4]       [,5]
##  [1,]  1.34185154  0.20359985 -0.005034602 -0.76186135 -1.0489687
##  [2,]  0.27001872  1.64266804  1.361292452  0.10048312 -0.8730561
##  [3,]  0.35729918 -1.02312092  1.514999869  0.42742738 -0.4395550
##  [4,]  2.02984054  0.40941469  1.078059341  1.12477940  0.3196170
##  [5,] -0.29178567 -0.31932897 -1.233155710 -0.48092053  0.3287599
##  [6,] -1.33367146 -0.03555508  0.008222698 -0.33403233 -0.1328199
##  [7,] -1.07762081 -1.01259814  0.804670049  0.53818671 -1.8376158
##  [8,] -0.68245981 -0.92572637  0.487013911 -0.13250592  0.1971838
##  [9,] -0.44350603  0.82192561  0.792913088  0.65469014 -0.9422564
## [10,]  0.48617600 -0.05174419 -0.700349933 -0.05280361  0.4678160
## [11,]  2.10351000  0.42367104  0.742110651  0.48480483  0.2923420
## [12,] -0.01478887  0.47969944 -0.045532817 -1.63538774  1.8060488
x[9, 3]
## [1] 0.7929131

Matrices

print(x)
##              [,1]        [,2]         [,3]        [,4]       [,5]
##  [1,]  1.34185154  0.20359985 -0.005034602 -0.76186135 -1.0489687
##  [2,]  0.27001872  1.64266804  1.361292452  0.10048312 -0.8730561
##  [3,]  0.35729918 -1.02312092  1.514999869  0.42742738 -0.4395550
##  [4,]  2.02984054  0.40941469  1.078059341  1.12477940  0.3196170
##  [5,] -0.29178567 -0.31932897 -1.233155710 -0.48092053  0.3287599
##  [6,] -1.33367146 -0.03555508  0.008222698 -0.33403233 -0.1328199
##  [7,] -1.07762081 -1.01259814  0.804670049  0.53818671 -1.8376158
##  [8,] -0.68245981 -0.92572637  0.487013911 -0.13250592  0.1971838
##  [9,] -0.44350603  0.82192561  0.792913088  0.65469014 -0.9422564
## [10,]  0.48617600 -0.05174419 -0.700349933 -0.05280361  0.4678160
## [11,]  2.10351000  0.42367104  0.742110651  0.48480483  0.2923420
## [12,] -0.01478887  0.47969944 -0.045532817 -1.63538774  1.8060488
x[, 1]
##  [1]  1.34185154  0.27001872  0.35729918  2.02984054 -0.29178567 -1.33367146
##  [7] -1.07762081 -0.68245981 -0.44350603  0.48617600  2.10351000 -0.01478887

Matrices

print(x)
##              [,1]        [,2]         [,3]        [,4]       [,5]
##  [1,]  1.34185154  0.20359985 -0.005034602 -0.76186135 -1.0489687
##  [2,]  0.27001872  1.64266804  1.361292452  0.10048312 -0.8730561
##  [3,]  0.35729918 -1.02312092  1.514999869  0.42742738 -0.4395550
##  [4,]  2.02984054  0.40941469  1.078059341  1.12477940  0.3196170
##  [5,] -0.29178567 -0.31932897 -1.233155710 -0.48092053  0.3287599
##  [6,] -1.33367146 -0.03555508  0.008222698 -0.33403233 -0.1328199
##  [7,] -1.07762081 -1.01259814  0.804670049  0.53818671 -1.8376158
##  [8,] -0.68245981 -0.92572637  0.487013911 -0.13250592  0.1971838
##  [9,] -0.44350603  0.82192561  0.792913088  0.65469014 -0.9422564
## [10,]  0.48617600 -0.05174419 -0.700349933 -0.05280361  0.4678160
## [11,]  2.10351000  0.42367104  0.742110651  0.48480483  0.2923420
## [12,] -0.01478887  0.47969944 -0.045532817 -1.63538774  1.8060488
x[3, ]
## [1]  0.3572992 -1.0231209  1.5149999  0.4274274 -0.4395550

Matrices

print(x)
##              [,1]        [,2]         [,3]        [,4]       [,5]
##  [1,]  1.34185154  0.20359985 -0.005034602 -0.76186135 -1.0489687
##  [2,]  0.27001872  1.64266804  1.361292452  0.10048312 -0.8730561
##  [3,]  0.35729918 -1.02312092  1.514999869  0.42742738 -0.4395550
##  [4,]  2.02984054  0.40941469  1.078059341  1.12477940  0.3196170
##  [5,] -0.29178567 -0.31932897 -1.233155710 -0.48092053  0.3287599
##  [6,] -1.33367146 -0.03555508  0.008222698 -0.33403233 -0.1328199
##  [7,] -1.07762081 -1.01259814  0.804670049  0.53818671 -1.8376158
##  [8,] -0.68245981 -0.92572637  0.487013911 -0.13250592  0.1971838
##  [9,] -0.44350603  0.82192561  0.792913088  0.65469014 -0.9422564
## [10,]  0.48617600 -0.05174419 -0.700349933 -0.05280361  0.4678160
## [11,]  2.10351000  0.42367104  0.742110651  0.48480483  0.2923420
## [12,] -0.01478887  0.47969944 -0.045532817 -1.63538774  1.8060488
x[c(1, 2), 4]
## [1] -0.7618614  0.1004831

Matrices

print(x)
##              [,1]        [,2]         [,3]        [,4]       [,5]
##  [1,]  1.34185154  0.20359985 -0.005034602 -0.76186135 -1.0489687
##  [2,]  0.27001872  1.64266804  1.361292452  0.10048312 -0.8730561
##  [3,]  0.35729918 -1.02312092  1.514999869  0.42742738 -0.4395550
##  [4,]  2.02984054  0.40941469  1.078059341  1.12477940  0.3196170
##  [5,] -0.29178567 -0.31932897 -1.233155710 -0.48092053  0.3287599
##  [6,] -1.33367146 -0.03555508  0.008222698 -0.33403233 -0.1328199
##  [7,] -1.07762081 -1.01259814  0.804670049  0.53818671 -1.8376158
##  [8,] -0.68245981 -0.92572637  0.487013911 -0.13250592  0.1971838
##  [9,] -0.44350603  0.82192561  0.792913088  0.65469014 -0.9422564
## [10,]  0.48617600 -0.05174419 -0.700349933 -0.05280361  0.4678160
## [11,]  2.10351000  0.42367104  0.742110651  0.48480483  0.2923420
## [12,] -0.01478887  0.47969944 -0.045532817 -1.63538774  1.8060488
x[c(3, 4), c(1, 2)]
##           [,1]       [,2]
## [1,] 0.3572992 -1.0231209
## [2,] 2.0298405  0.4094147

Lists

A list is an ordered collection of objects known as components, which can be from different classes.

my.list <- list(name = "T Rex", period = "Cretaceous", dinos.eaten = c(8, 12, 18))
print(my.list)
## $name
## [1] "T Rex"
## 
## $period
## [1] "Cretaceous"
## 
## $dinos.eaten
## [1]  8 12 18

Lists

The elements of a list are always enumerated. To access to each level of a list:

my.list$name
## [1] "T Rex"
my.list$period
## [1] "Cretaceous"
my.list$dinos.eaten
## [1]  8 12 18

Also, they can be accessed using their respective position:

my.list[[3]]
## [1]  8 12 18

Why double square brackets?

Lists

my.list[[3]]
## [1]  8 12 18

Now that we have the numbers for the dinos.eaten, how can we extract the 12?

a <- my.list[[3]]
print(a)
## [1]  8 12 18
a[2]
## [1] 12

# OR:
my.list[[3]][2]
## [1] 12

Data frames

A data frame is a table or a two-dimensional array-like structure in which each column contains values of one variable and each row contains one set of values from each column.

  1. The column names should be non-empty
  2. The row names should be unique
  3. The data stored in a data frame can be numeric, factor, or character
  4. Each column must have the same number of data items

Data frames

To construct a data frame, the function data.frame is used. - As an example, we have the following information:

River Length(km) Discharge (m\(^3\)/s)
Congo 4370 41200
Mekong 4023 16000
Rhine 1233 2900
river.data <- data.frame(River = c("Congo", "Mekong", "Rhine"),
                         Length = c(4370, 4023, 1233),
                         Discharge = c(41200, 16000, 2900))
print(river.data)
##    River Length Discharge
## 1  Congo   4370     41200
## 2 Mekong   4023     16000
## 3  Rhine   1233      2900

Data frames

Using the $ symbol, we can extract data from a certain column.

river.data$Length
## [1] 4370 4023 1233
river.data$Length[3]
## [1] 1233
river.data[,2]
## [1] 4370 4023 1233

Data frames

The $ sign can also be used to create a new data column. Here, we will convert discharge from m 3 /s into BMC/year and store it in a new data column.

river.data$Discharge_BCMyear <- river.data$Discharge * 365.25* 24 * 60 * 60 / 10^9
print(river.data)
##    River Length Discharge Discharge_BCMyear
## 1  Congo   4370     41200        1300.17312
## 2 Mekong   4023     16000         504.92160
## 3  Rhine   1233      2900          91.51704

Scripts and packages

Running Scripts

  • The command Ctrl + Enter will run (same as the “Run” button on the top right of the script area):
  • The line of the code that you are on
  • All the code that is selected by clicking and dragging
  • The command Ctrl + Shift + s will run the entire script (same as the Source button on the top right of the script area)